package excelintegration;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.h2.constant.SysProperties;

import db.CustomerDBInteraction;
import db.DBConnection;
import db.DBInteraction;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class RiepilogoDatiInserimentoJPanel extends javax.swing.JPanel {
	private JLabel cognomejLabel;
	private JTextField cognomeNomejTextField;
	private JLabel IndirizzoJLabel;
	private JButton salvajButton;
	private JRadioButton personaFisicajRadioButton;
	private JRadioButton aziendajRadioButton;
	private JPanel radiojPanel;
	private JPanel nomeCognomejPanel;
	private JLabel nomejLabel;
	private JLabel jLabel1;
	private JTextField paroleCognomejTextField;
	private JLabel paroleCognomejLabel;
	private JTextField comunejTextField;
	private JLabel comunejLabel;
	private JTextField indirizzojTextField;
	private JTextField codiceFiscalejTextField;
	private JLabel codiceFiscalejLabel;
	
	private String cognomeNome, indirizzo, comune, codicefiscale, cognome, nome;
	private int nParoleCognome;
	
	private DBConnection dbCon;
	
	private String line;
	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new RiepilogoDatiInserimentoJPanel("", ""));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public RiepilogoDatiInserimentoJPanel(String line, String comune) {
		super();
		this.line = line;
		this.comune = comune;
		this.cognome = "";
		this.nome = "";
		try {
			dbCon = new DBConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initGUI();
		initFromInput();
	}
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
			thisLayout.rowHeights = new int[] {7, 7, 7, 7, 7, 7, 7, 7};
			thisLayout.columnWeights = new double[] {0.0, 0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {168, 7, 7, 7};
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(616, 366));
			{
				cognomejLabel = new JLabel();
				this.add(cognomejLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				cognomejLabel.setText("Cognome e Nome");
			}
			{
				cognomeNomejTextField = new JTextField();
				this.add(cognomeNomejTextField, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				codiceFiscalejLabel = new JLabel();
				this.add(codiceFiscalejLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				codiceFiscalejLabel.setText("Codice Fiscale");
			}
			{
				codiceFiscalejTextField = new JTextField();
				this.add(codiceFiscalejTextField, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				IndirizzoJLabel = new JLabel();
				this.add(IndirizzoJLabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				IndirizzoJLabel.setText("Indirizzo");
			}
			{
				indirizzojTextField = new JTextField();
				this.add(indirizzojTextField, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				comunejLabel = new JLabel();
				this.add(comunejLabel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				comunejLabel.setText("Comune");
			}
			{
				comunejTextField = new JTextField();
				this.add(comunejTextField, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
			}
			{
				paroleCognomejLabel = new JLabel();
				this.add(paroleCognomejLabel, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				paroleCognomejLabel.setText("Parole Cognome");
			}
			{
				paroleCognomejTextField = new JTextField();
				this.add(paroleCognomejTextField, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				paroleCognomejTextField.setPreferredSize(new java.awt.Dimension(57, 23));
				paroleCognomejTextField.addKeyListener(new KeyListener() {
					
					@Override
					public void keyTyped(KeyEvent arg0) {
						
						
					}
					
					@Override
					public void keyReleased(KeyEvent arg0) {
						if(arg0.getKeyCode() == KeyEvent.VK_BACK_SPACE)
						{
							cognome = "";
							nome = "";
							nomejLabel.setText("nome: " + nome);
							jLabel1.setText("cognome: " + cognome);
						}
						else
						{
							System.out.println(arg0.getKeyCode());
							System.out.println(arg0.getKeyChar());
							System.out.println(arg0.getKeyText(arg0.getKeyCode()));
							String character = "" + arg0.getKeyChar();
							if("123456789".contains(character))
							//if(arg0.getKeyCode() >= KeyEvent.VK_0 && arg0.getKeyCode() <= KeyEvent.VK_9)
							{
								System.out.println("Sono entrato");
								String [] temp = cognomeNome.split(" ");
								int  barrier = Integer.parseInt(paroleCognomejTextField.getText());
								for(int i=0; i<temp.length; i++)
								{
									if(i<barrier)
										cognome = cognome + temp[i] + " ";
									else
										nome = nome + temp[i] + " ";
								}
								nomejLabel.setText("nome: " + nome);
								jLabel1.setText("cognome: " + cognome);
							}
						}
						
					}
					
					@Override
					public void keyPressed(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
				});
			}
			{
				nomeCognomejPanel = new JPanel();
				BoxLayout nomeCognomejPanelLayout = new BoxLayout(nomeCognomejPanel, javax.swing.BoxLayout.Y_AXIS);
				nomeCognomejPanel.setLayout(nomeCognomejPanelLayout);
				this.add(nomeCognomejPanel, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				{
					nomejLabel = new JLabel();
					nomeCognomejPanel.add(nomejLabel);
					nomejLabel.setText("nome:");
				}
				{
					jLabel1 = new JLabel();
					nomeCognomejPanel.add(jLabel1);
					jLabel1.setText("cognome");
				}
			}
			{
				radiojPanel = new JPanel();
				FlowLayout radiojPanelLayout = new FlowLayout();
				radiojPanelLayout.setAlignOnBaseline(true);
				radiojPanel.setLayout(radiojPanelLayout);
				this.add(radiojPanel, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				{
					personaFisicajRadioButton = new JRadioButton();
					radiojPanel.add(personaFisicajRadioButton);
					personaFisicajRadioButton.setText("Persona Fisica");
					personaFisicajRadioButton.setSelected(true);
					personaFisicajRadioButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							if(personaFisicajRadioButton.isSelected())
							{
								personaFisicajRadioButton.setSelected(false);
								aziendajRadioButton.setSelected(true);
							}
							else
							{
								personaFisicajRadioButton.setSelected(true);
								aziendajRadioButton.setSelected(false);
							}
						}
					});
				}
				{
					aziendajRadioButton = new JRadioButton();
					radiojPanel.add(aziendajRadioButton);
					aziendajRadioButton.setText("Azienda");
					aziendajRadioButton.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent arg0) {
							// TODO Auto-generated method stub
							if(aziendajRadioButton.isSelected())
							{
								personaFisicajRadioButton.setSelected(true);
								aziendajRadioButton.setSelected(false);
							}
							else
							{
								personaFisicajRadioButton.setSelected(false);
								aziendajRadioButton.setSelected(true);
							}
						}
					});
				}
			}
			{
				salvajButton = new JButton();
				this.add(salvajButton, new GridBagConstraints(3, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				salvajButton.setText("Salva");
				salvajButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/icons/save-icon32.png")));
				salvajButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						// TODO Auto-generated method stub
						String [] info = {"Persona Fisica", cognome, nome, codiceFiscalejTextField.getText(), "", "",
											comunejTextField.getText(), "", "", "", "false", "false", "true", "true", "2010"};
						CustomerDBInteraction.SaveNewCustomerWithoutDate(info, dbCon);
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void initFromInput()
	{
		String [] temp = line.split(" - ");
		this.cognomeNome = temp[0];
		this.codicefiscale = temp[1];
		this.indirizzo = temp[2];
		 
		if(this.indirizzo.toLowerCase().contains(this.comune.toLowerCase()))
			this.indirizzo.toLowerCase().replace(this.comune.toLowerCase(), "");
		
		if(this.cognomeNome.contains("*"))
			this.cognomeNome = this.cognomeNome.substring(0, this.cognomeNome.length()-1);
		
		if(this.codicefiscale.length() != 16)
			this.codicefiscale = "";
		
		this.cognomeNomejTextField.setText(cognomeNome);
		this.codiceFiscalejTextField.setText(codicefiscale);
		this.indirizzojTextField.setText(indirizzo);
		this.comunejTextField.setText(comune);
	}

}
